pub(crate) const BUF_CAP: usize = 4096;

#[cfg(test)]
mod tests {
    use crate::resp::array::RespArray;
    use crate::resp::buck_string::BuckString;
    use crate::resp::map::RespMap;
    use crate::resp::set::RespSet;
    use crate::resp::simple_error::SimpleError;
    use crate::resp::simple_string::SimpleString;
    use crate::resp::{RespEncode, RespFrame};

    #[test]
    fn test_simple_string_encode() {
        let frame: RespFrame = SimpleString("hello".to_string()).into();
        assert_eq!(frame.encode(), b"+hello\r\n");
    }

    #[test]
    fn test_error_encode() {
        let frame: RespFrame = SimpleError::new("Error message".to_string()).into();
        assert_eq!(frame.encode(), b"-Error message\r\n");
    }

    #[test]
    fn test_integer_encode() {
        let frame: RespFrame = 123.into();
        let v = frame.encode();
        assert_eq!(v, b":+123\r\n");
        let frame: RespFrame = (-123).into();
        let v = frame.encode();
        assert_eq!(v, b":-123\r\n");
    }

    #[test]
    fn test_double_encode() {
        let frame: RespFrame = 123.456.into();
        let v = frame.encode();
        println!("{:?}", String::from_utf8_lossy(&v));
        assert_eq!(v, b",+123.456\r\n");
        let frame: RespFrame = (-123.456).into();
        assert_eq!(frame.encode(), b",-123.456\r\n");

        let frame: RespFrame = 1.23456e+8.into();
        let v = frame.encode();
        println!("{:?}", String::from_utf8_lossy(&v));
        assert_eq!(v, b",+1.23456e8\r\n");

        let frame: RespFrame = (-1.23456e-9).into();
        let v = frame.encode();
        println!("{:?}", String::from_utf8_lossy(&v));
        assert_eq!(v, b",-1.23456e-9\r\n");
    }

    #[test]
    fn test_array_encode() {
        let frame: RespFrame = RespArray::new(vec![
            BuckString::new("set".to_string()).into(),
            BuckString::new("hello".to_string()).into(),
            BuckString::new("world".to_string()).into(),
        ])
        .into();
        let v = frame.encode();
        println!("{:?}", String::from_utf8_lossy(&v));
        assert_eq!(v, b"*3\r\n$3\r\nset\r\n$5\r\nhello\r\n$5\r\nworld\r\n");
    }

    #[test]
    fn test_map_encode() {
        let mut map = RespMap::new();
        map.insert(
            "hello".to_string(),
            BuckString::new("world".to_string()).into(),
        );
        map.insert("foo".to_string(), (-123456.789).into());
        let frame: RespFrame = map.into();
        let v = frame.encode();
        println!("{:?}", String::from_utf8_lossy(&v));
        assert_eq!(
            v,
            b"%2\r\n+foo\r\n,-123456.789\r\n+hello\r\n$5\r\nworld\r\n"
        );
    }

    #[test]
    fn test_set_encode() {
        let frame: RespFrame = RespSet::new(vec![
            RespArray::new([1234.into(), true.into()]).into(),
            SimpleString::new("world".to_string()).into(),
        ])
        .into();
        let v = frame.encode();

        println!("{:?}", String::from_utf8_lossy(&v));
        assert_eq!(v, b"~2\r\n*2\r\n:+1234\r\n#t\r\n+world\r\n");
    }
}
